import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import {OpenSerialRequest} from "../models/up_request";

const defaultFormData: OpenSerialRequest = {
    device: '',
    baudrate: 115200,
    parity: 'none',
    databits: 8,
    stopbits: 1,
    flowcontrol: 'none',
    timeout: 10,
};

export interface SerialFormState {
    formData: OpenSerialRequest;
    setFormData: (data: OpenSerialRequest) => void;
    resetFormData: () => void;
}

export const useSerialFormStore = create<SerialFormState>()(
    persist(
        (set, get) => ({
            formData: defaultFormData,
            setFormData: (data) => set({ formData: data }),
            resetFormData: () => set({ formData: defaultFormData }),
        }),
        {
            name: 'serial-form-storage',
            storage: createJSONStorage(() => localStorage),
        }
    )
);